
import { LoginParam, LoginReturn, User, UserListType, UserListParam, PutUserStateParam } from "@Tea/model"
import promisePool from "./index"
import { SelectReturn } from "../types"
import { InitSelectWhereType, renderSelectParam, renderSelectSql, renderUpdateSql } from "../utils/renderSql"
import { ReposBase } from "./RepoBase"
export class UserRepository extends ReposBase {
  // 缓存User
  private users: Record<LoginParam["account"], User> = {}

  public static inst = new UserRepository()
  public static getInstance() {
    return UserRepository.inst
  }

  /**
   * @vuese 函数重载，获取User
   */
  public getUser(account: string, password: string) {
    const config: renderSelectParam = {
      database: [
        { name: "users" }
      ],
      returnFeild: [
        { name: "user_id", aliasName: "userId" }, 'username', 'account', 'avator', 'enable', "type"
      ],
      where: [
        { key: "account", sqlVal: account },
        { key: "password", sqlVal: password },
      ]
    }

    return this.query<LoginReturn>(renderSelectSql(config))
  }

  /**
   * @use 获取用户列表
   * @param params 
   * @returns 
   */
  async getUserList(params: UserListParam, type: "admin" | "user") {
    const sql = renderSelectSql({
      returnFeild: [
        { name: 'user_id', aliasName: "userId" },
        "username", 'account', 'avator', 'enable'
      ],
      database: [{ name: "users" }],
      where: [
        { key: "enable", sqlVal: params.enable },
        { key: "type", sqlVal: type }
      ],
      like: [
        { key: "username", sqlVal: params.username },
      ]
    })

    return this.pagingQuery(sql, params)
  }

  /**
   * @use 改变用户状态
   */
  putUserState(params: PutUserStateParam) {
    const sql = renderUpdateSql({
      database: "users",
      keyAndVal: [{
        key: "enable",
        value: params.enable
      }],
      where: [{
        key: "user_id",
        "sqlVal": params.userId
      }]
    })

    return this.update(sql)
  }

  /**
   * @use 获取禁用的成员
   */
  getDeactivateUser() {
    const sql = renderSelectSql({
      returnFeild: [
        { name: 'user_id', aliasName: "userId" },
      ],
      database: [{ name: "users" }],
      where: [{ key: "enable", sqlVal: 0 }]
    })

    return this.query<{ userId: number }>(sql)
  }

  /**
   * @use 改变权限
   */
  changAuthority(userId: number, type: "admin" | "user") {
    const updateSql = renderUpdateSql({
      database: "users",
      where: [
        {
          key: "user_id", sqlVal: userId
        }
      ],
      keyAndVal: [
        { key: "type", value: type }
      ]
    })

    return this.update(updateSql)
  }

  /**
   * @use 注册
   * @param account 
   * @returns 
   */
  findAccount(account: string) {
    const sql = `SELECT user_id FROM users WHERE account = '${account}'`
    return this.query<UserListType>(sql)
  }

  register(account: string, username: string, password: string) {
    const sql = `INSERT INTO users(account, username, password, enable, type) VALUES('${account}', '${username}', '${password}', 1, 'user')`

    return this.insert(sql)
  }
}